home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / ASTRONOM / H139.ZIP / RO101.ZIP / RO_TEXT.C < prev    next >
C/C++ Source or Header  |  1991-11-04  |  8KB  |  337 lines

  1. /********************************************************/
  2. /*                            */
  3. /*    ro_text.c    text-handling routines for ro    */
  4. /*                            */
  5. /*    ro version 1.10                    */
  6. /*                            */
  7. /*    Portions copyright (c) 1989 by Ted A. Campbell    */
  8. /*        Bywater Software            */
  9. /*        P. O. Box 4023                */
  10. /*        Duke Station                */
  11. /*        Durham, NC  27706            */
  12. /*                            */
  13. /*    Contains portions of ROFF4, Version 1.61    */
  14. /*      (c) 1985 by Konrad Kwok                         */
  15. /*        20 3rd Street, Section M        */
  16. /*        Fariview Park,                */
  17. /*        Hong Kong                */
  18. /*                            */
  19. /*      Contains portions of ROFF4, Version 1.60        */
  20. /*      (c) 1983, 4 by Ernest E. Bergmann               */
  21. /*              Physics, Building #16                   */
  22. /*              Lehigh University                       */
  23. /*              Bethlehem, Pa. 18015                    */
  24. /*                                                      */
  25. /*    ro and its predecessor ROFF4 are based on     */
  26. /*    the ROFF text processor described in Kernigan    */
  27. /*    and Plauger's now-classic text <Software Tools> */
  28. /*                            */
  29. /* Permission is hereby granted for all commercial and    */
  30. /* non-commercial reproduction and distribution of this */
  31. /* material provided this notice is included.        */
  32. /*                            */
  33. /********************************************************/
  34.  
  35. #include "ro.h"
  36.  
  37. /**************************************************
  38. handles case of leading blanks or tabs; empty lines 
  39. ***************************************************/
  40.  
  41. leadbl( line )
  42.    char *line;
  43.    {
  44.    int i, j, white;
  45.  
  46.    ro_brk();
  47.  
  48.    for ( white = 0, i = 0; line[i] == ' ' || line[i] == '\t'; i++ )
  49.       {
  50.       if (line[i] == ' ')
  51.          {
  52.          white++;
  53.          }
  54.       else
  55.          {
  56.          white = ro_min( ro_rmval[0], ro_tabsiz[0]*(1+white/ro_tabsiz[0]));
  57.          }
  58.       }
  59.  
  60.    if ( line[i] != NEWLINE )
  61.       {
  62.       ro_tival += white;
  63.       }
  64.  
  65.    /* move line to left */
  66.  
  67.    for( j = 0; (line[j] = line[i]) != '\0'; j++, i++ );
  68.  
  69.    }
  70.  
  71. /*****************************************
  72. puts out page header
  73. ******************************************/
  74.  
  75. phead()
  76.    {
  77.    exchange();
  78.    ro_uf=ro_xf=FALSE;
  79.    ro_mcnt=1;
  80.    ro_curpag = ro_newpag++;
  81.    preregister( "%", ro_curpag );
  82.    if((ro_curpag<ro_firstpage)||(ro_curpag>ro_lastpage))
  83.       ro_suppress=TRUE;
  84.    else
  85.       ro_suppress=FALSE;
  86.    ro_oldln=-1;
  87.    if( ro_pagestop )
  88.       {
  89.       fprintf( stderr, "%cPAGE:  Please type <RETURN> to continue:  ",
  90.          BELL );
  91.       getchar();       /* wait until char typed at console */ 
  92.       }
  93.    ro_plineno=0;
  94.    ro_pflineno=0;
  95.    if (ro_m1val[0] > 0 )
  96.       { 
  97.       ro_vlineno = ro_m1val[0]-1;
  98.       if (ro_curpag % 2) puttl3 ( ro_ohead, ro_oh2, ro_oh3, ro_curpag );
  99.       else puttl3 ( ro_ehead, ro_eh2, ro_eh3, ro_curpag );
  100.       }
  101.    ro_vlineno = ro_m1val[0]+ro_m2val[0]; 
  102.    ro_vflineno=0;
  103.    exchange();
  104.    }
  105.  
  106. /**************************************
  107. puts out page footer
  108. ***************************************/
  109.  
  110. pfoot()
  111.    {
  112.    exchange();
  113.    ro_uf=ro_xf=FALSE;
  114.    ro_mcnt=1;
  115.    ro_vlineno = ro_bottom+ro_m3val[0]; 
  116.    ro_vflineno=0;
  117.    if ( ro_m4val[0] > 0 )
  118.       { 
  119.       if(ro_curpag % 2)puttl3 ( ro_ofoot,ro_of2,ro_of3, ro_curpag );
  120.       else puttl3(ro_efoot,ro_ef2,ro_ef3,ro_curpag);
  121.       }
  122.    ro_vlineno = ro_plval[0];
  123.    ro_vflineno=0;
  124.    if (ro_useff) ro_outc(FORMF);
  125.    else padv();
  126.    ro_oldbot=ro_plineno=ro_pflineno=0; /*printer at start of newpage*/
  127.    ro_oldln=-1;
  128.    exchange();
  129.    }
  130.  
  131. /**********************************************
  132. space n lines or to bottom of the page
  133. ***********************************************/
  134.  
  135. do_space( n )
  136.    int n;
  137.    {
  138.    ro_brk(); /* flush out last unfilled line */
  139.    if ( ro_vlineno >= ro_bottom)   
  140.       {
  141.       return; /* end of page */
  142.       }
  143.    if ( ro_vlineno < 0) 
  144.       {
  145.       phead(); /* top of page */
  146.       }
  147.    ro_vlineno += n;
  148.  
  149.    if ( ro_vlineno >= ro_bottom ) 
  150.       {
  151.       pfoot(); /* print footer if bottom */
  152.       }
  153.    }
  154.  
  155. /*******************************************************/
  156.  
  157. text (line)
  158.    char *line;
  159.    {
  160.    char wrdbuf [LSZ];
  161.  
  162. #ifdef   DEBUG
  163.    if ( ro_debug == TRUE ) 
  164.       {
  165.       fprintf( stderr, "DEBUG:  text: <%s> \n", line);
  166.       }
  167. #endif
  168.  
  169.    if (line[0] == BLANK || line[0]==NEWLINE || line[0] == TAB)
  170.       leadbl (line);
  171.    if (ro_ceval > 0)
  172.       { 
  173.       center(line);
  174.       put(line);
  175.       ro_ceval--;
  176.       }
  177.    else if ( ( line[0] == NEWLINE ) || ( line[0] == '\0' ))
  178.       {
  179.       do_space( ro_lsval[0] );
  180.       }
  181.    else if(!ro_fill[0])
  182.       put(line);
  183.    else
  184.       while (WE_HAVE_A_WORD == getwrd (line, wrdbuf))
  185.          putwrd (wrdbuf);
  186.    }
  187.  
  188. /******************************************************
  189.  
  190. put out a line of text with correct indentation
  191.  
  192. *******************************************************/
  193.  
  194. put (line)
  195.    char *line;
  196.    {
  197.    int fs, minfs;
  198.  
  199.    if (( ro_vlineno < 0 ) || ( ro_vlineno >= ro_bottom ))
  200.       {
  201.       phead();
  202.       }
  203.    fs = ( ro_vlineno - ro_oldln ) * ro_frval;
  204.    minfs = ro_oldbot - ro_outtop;
  205.    if ( !( ro_oldbot && ro_outtop ))
  206.       {
  207.       minfs++;
  208.       }
  209.    while ( fs < minfs )
  210.       {
  211.       fs += ro_frval;
  212.       ro_vlineno++;
  213.       }
  214.    need( 0 );
  215.    putline( line );
  216.    ro_tival = ro_inval[0];
  217.    ro_vlineno += ro_lsval[0];
  218.    if ( ro_vlineno >= ro_bottom )
  219.       {
  220.       pfoot();
  221.       }
  222.    }
  223.  
  224. /***********************************************************
  225.  
  226. concatenates the word onto the end of ro_outbuf for filled text
  227.  
  228. ************************************************************/
  229.  
  230. putwrd (wrdbuf) /*Nov 22: ro_sentence*/
  231.    char *wrdbuf;
  232.    {
  233.    int line_len, new_out_width, wid;
  234.    int nextra;
  235.  
  236.    skip_blanks (wrdbuf); 
  237.    trunc_bl (wrdbuf);
  238.    wid =1+ro_sentence+strln3(wrdbuf,TRUE,1);/*sets ro_wtop,ro_wbot*/
  239.    line_len = ro_rmval[0] - ro_tival;
  240.    new_out_width = ro_outw+wid;
  241.    if (new_out_width > ro_min(line_len, MAXLINE-1))
  242.       { 
  243.       nextra = ro_min(line_len, MAXLINE-1)-ro_outw+1;
  244.       if(ro_outbuf[ro_outpos-2]==BLANK) nextra++;
  245.       if(ro_adjust) spread(ro_outbuf,nextra,ro_outwrds);
  246.       ro_brk();
  247.       }
  248.    ro_outw += wid;
  249.    OUTcat(wrdbuf);
  250.    OUTSP(); 
  251.    if(ro_sentence) OUTSP();
  252.    ro_outwrds++;
  253.    if(ro_wtop<ro_outtop) ro_outtop=ro_wtop;
  254.    if(ro_wbot>ro_outbot) ro_outbot=ro_wbot;
  255.    }
  256.  
  257. /**********************************************************
  258.  
  259. a new putline routine
  260.  
  261. ***********************************************************/
  262.  
  263. putline (line)
  264.    char *line;
  265.    {
  266.    char c;
  267.    blanks ( ro_tival );
  268.    for ( ; c = *line; line++ )
  269.       {
  270.       putout( c );
  271.       }
  272.    putout( '\0' );
  273.    printout();
  274.    }
  275.  
  276. /****************************************/
  277.  
  278. OUTcat(str)    /*appends string to ro_outbuf*/
  279.    char *str;
  280.    {
  281.    while(ro_outbuf[ro_outpos]=*(str++))
  282.       ro_outpos++;
  283.    }
  284.  
  285. /****************************************/
  286.  
  287. OUTSP()       /*appends BLANK to ro_outbuf*/
  288.    {
  289.    ro_outbuf[ro_outpos++]=BLANK;
  290.    ro_outbuf[ro_outpos]='\0';
  291.    }
  292.  
  293. /****************************************/
  294.  
  295. gloss() /*prints on STDOUT a glossary of .tr chars*/
  296.    {
  297.    int i;
  298.    char line[20],tcs;
  299.    put("GLOSSARY:");
  300.    put("USE    <GET>");
  301.    tcs=ro_tcval[0];
  302.    ro_tcval[0] |= 128; /*set most significant bit*/
  303.    for(i=1;i<19;i++) line[i]=' ';
  304.    line[0]=tcs;
  305.    line[8]='<';
  306.    line[9]=ro_tcval[0];
  307.    line[11]='>';
  308.    line[12]='\0';
  309.    for(i=' ';i<127;i++)
  310.       {
  311.       if(ro_tptr[i-' '])
  312.          {
  313.          line[1]=line[10]=i;
  314.          put(line);
  315.          }
  316.       }
  317.    ro_tcval[0]=tcs;
  318.    do_space(HUGE);
  319.    }
  320.  
  321. /***************************************/
  322.  
  323. exchange() /*maintain separate environment for headers and
  324.       footers*/
  325.    {
  326.    int i;
  327.    i=ro_mcnt2; 
  328.    ro_mcnt2=ro_mcnt; 
  329.    ro_mcnt=i;
  330.    i=ro_xf2; 
  331.    ro_xf2=ro_xf; 
  332.    ro_xf=i;
  333.    i=ro_uf2; 
  334.    ro_uf2=ro_uf; 
  335.    ro_uf=i;
  336.    }
  337.